package com.zhangck.bos2_0.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.components.Push;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhangck.bos2_0.Service.AreaService;
import com.zhangck.bos2_0.common.BaseAction;
import com.zhangck.bos2_0.utils.PinYin4jUtils;

import cn.itcast.bos.domain.base.Area;

/** 
* @author 上午茶
* @version 创建时间：2018年5月30日 下午6:40:21 
* 类说明 
*/

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class AeraAction  extends BaseAction<Area> {

	
	
	@Autowired
	private AreaService areaService;
	
	private File file;
	
	
	public void setFile(File file) {
		this.file = file;
	}


	@Action(value="Aeraupload_action")
	public String save() throws FileNotFoundException, IOException{
			List<Area> list = new ArrayList<>();
			
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
			
			HSSFSheet sheet = workbook.getSheetAt(0);
			
			for (Row row : sheet) {
				Area area = new Area();
				area.setId(row.getCell(0).getStringCellValue());
				area.setProvince(row.getCell(1).getStringCellValue());
				area.setCity(row.getCell(2).getStringCellValue());
				area.setDistrict(row.getCell(3).getStringCellValue());
				area.setPostcode(row.getCell(4).getStringCellValue());
				list.add(area);
				
				//得到每个属性值
				String province = area.getProvince();
				String city = area.getCity();
				String district = area.getDistrict();
				System.out.println(province);
				//进行切割
				 province = province.substring(0, province.length()-1);
				 city = city.substring(0, city.length()-1);
				 district = district.substring(0, district.length()-1);
				System.out.println(province);
				
				//取汉子首字母
				String[] headString = PinYin4jUtils.getHeadByString(province+city+district);
				StringBuilder builder = new StringBuilder();
				for (String string : headString) {
					builder.append(string);
				}
				String shortcode = builder.toString();
				area.setShortcode(shortcode);
				
				//将汉字转换成拼音
				String headByString = PinYin4jUtils.hanziToPinyin(city, "");
				area.setCitycode(headByString);
			}
			areaService.seveBacth(list);
		return  NONE;
	}
	
	
	
	//分页
		@Action(value="AreadAction_page",results={@Result(name="success",type="json")})
		public String  AreadAction_page(){
			Pageable request = new PageRequest(page-1, rows);
			Specification<Area> specification = new Specification<Area>() {
			
				@Override
				public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					List<Predicate> list = new ArrayList<>();
					//等值查询
					if (StringUtils.isNotBlank(model.getProvince())) {
						Predicate p1 = cb.equal(root.get("province").as(String.class),
								model.getProvince());
						list.add(p1);
					}
					//模糊查询
					if (StringUtils.isNotBlank(model.getCity())) {
						Predicate p2 = cb.like(root.get("city").as(String.class), 
								"%"+model.getCity()+"%");
						list.add(p2);
					}
					//等值查询
					if (StringUtils.isNotBlank(model.getDistrict())) {
						Predicate p3 = cb.equal(root.get("type").as(String.class),
								model.getDistrict());
						list.add(p3);
					}
					
					return cb.and(list.toArray(new Predicate[0]));
				}
			};
			
			
			Page<Area> page1 = areaService.findPageData(specification,request);

			pushPageDataValueStack(page1);
			
			return  "success";
		}


	
}
